Plotly Offline brings interactive Plotly graphs to the offline IPython Notebook environment.
Instead of saving the graphs to a server, your data and graphs will remain inside your IPython notebook. When your ready to share, you can just publish them to the Plotly Cloud or to your company's internal Plotly Enterprise.
To get started with Plotly Offline, start a trial or visit purchasing to learn about licensing and receive a URL to download the package.
! pip install plotly --upgrade
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
print __version__ # requires version >= 1.7.5
download_plotlyjs('your_licensed_plotly_offline_url') # Visit http://purchasing.plot.ly/plotly-offline-ipython
init_notebook_mode() # run at the start of every ipython notebook to use plotly.offline
iplot([{"x": [1, 2, 3], "y": [3, 1, 6]}])
from plotly.graph_objs import *
import numpy as np
iplot([Box(y = np.random.randn(50), showlegend=False) for i in range(45)])
x = np.random.randn(2000)
y = np.random.randn(2000)
iplot([Histogram2dContour(x=x, y=y, contours=Contours(coloring='heatmap')),
Scatter(x=x, y=y, mode='markers', marker=Marker(color='white', size=3, opacity=0.3))])
# Plotting with Pandas
import pandas as pd
df = pd.read_csv('https://plot.ly/~etpinard/191.csv')
df.head(1)
iplot({
'data': [
Scatter(x=df[continent+'_Life Expentancy [in years]'],
y=df[continent+'_Gross Domestic Product per Capita [in USD of the year 2000]'],
text=df[continent+'_text'],
marker=Marker(size=df[continent+'_marker.size'], sizemode='area', sizeref=131868,),
mode='markers',
name=continent) for continent in ['Africa', 'Americas', 'Asia', 'Europe', 'Oceania']
],
'layout': Layout(xaxis=XAxis(title='Life Expectancy'), yaxis=YAxis(title='GDP per Capita', type='log'))
})
# An alternative method for Pandas plotting is with cufflinks: https://github.com/santosjorge/cufflinks
! pip install cufflinks --upgrade
import cufflinks as cf
iplot(cf.datagen.lines().iplot(asFigure=True,
kind='scatter',xTitle='Dates',yTitle='Returns',title='Returns'))
iplot(cf.datagen.heatmap(20,20).iplot(asFigure=True,
kind='heatmap',colorscale='spectral',title='Cufflinks - Heatmap'))
import pandas as pd
df_airports = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv')
df_airports.head()
df_flight_paths = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv')
df_flight_paths.head()
airports = [ dict(
type = 'scattergeo',
locationmode = 'USA-states',
lon = df_airports['long'],
lat = df_airports['lat'],
hoverinfo = 'text',
text = df_airports['airport'],
mode = 'markers',
marker = dict(
size=2,
color='rgb(255, 0, 0)',
line = dict(
width=3,
color='rgba(68, 68, 68, 0)'
)
))]
flight_paths = []
for i in range( len( df_flight_paths ) ):
flight_paths.append(
dict(
type = 'scattergeo',
locationmode = 'USA-states',
lon = [ df_flight_paths['start_lon'][i], df_flight_paths['end_lon'][i] ],
lat = [ df_flight_paths['start_lat'][i], df_flight_paths['end_lat'][i] ],
mode = 'lines',
line = dict(
width = 1,
color = 'red',
),
opacity = float(df_flight_paths['cnt'][i])/float(df_flight_paths['cnt'].max()),
)
)
layout = dict(
title = 'Feb. 2011 American Airline flight paths<br>(Hover for airport names)',
showlegend = False,
height = 800,
geo = dict(
scope='north america',
projection=dict( type='azimuthal equal area' ),
showland = True,
landcolor = 'rgb(243, 243, 243)',
countrycolor = 'rgb(204, 204, 204)',
),
)
fig = dict( data=flight_paths + airports, layout=layout )
iplot(fig)
import plotly.plotly as py # all methods in plotly.plotly will communicate with a Plotly Cloud or Plotly Enterprise
# get_figure downloads a figure from plot.ly or Plotly Enterprise.
# You need to provide credentials to download figures: https://plot.ly/python/getting-started/
fig = py.get_figure('https://plot.ly/~jackp/8715', raw=True)
iplot(fig)